﻿/**
 * Date Utility Class
 * Copyright (c) 2009 designmarco
 * @author Marco Di Giuseppe  |  http://designmarco.com
 * marco[at]designmarco[dot]com
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package com.designmarco.utils
{
	/**
	 * DateUtil provides convenient static methods to assist when
	 * working with the Date Object.
	 */
	public class DateUtil
	{
		/**
		 * Covert a Date Object into a readable timestamp format
		 * @param  date 		Date Object
		 * @return timeStamp	m/d/yyyy  0:00 am/pm
		 */
		public static function getTimeStamp(date:Date):String
		{
			return	getDate(date) + "  " + getTime(date);
		}
		
		/**
		 * Gets the formatted date part of a date object (mm/dd/yyyy)
		 * @param  date  	Date Object
		 * @return formatted string of todays date
		 */
		public static function getDate(value:Date):String
		{
			return String(value.month + 1) + "/" + String(value.date) + "/" + String(value.fullYear);
		}
		
		/**
		 * Gets the formatted time part of a date object (hh:mm:ss)
		 * @param date 	Date Object
		 * @return formatted string of the current time
		 */
		public static function getTime(value:Date):String
		{
			var h:int = value.hours;
			return	String(standardize(h)) + ":" + pad(value.minutes) + " " + ((h > 11)? "pm" : "am");
		}
		
		/**
		 * Convert 24 hour time standard to 12 hour
		 * @param  date  number to process
		 * @return 12 hour time standard conversion
		 */
		public static function standardize(value:Number):Number
		{
			var n:int = (value > 12)? value - 12 : value;
			n = (n == 0)? 12 : n;
			return  n
		}
		
		/**
		 * Adds a leading 0 to the passed value before converting to a String
		 * @param	n	value to clean
		 * @return  formatted string with leading 0
		 */
		public static function pad(n:int):String
		{
			return (String( n ).length < 2)? String( "0" + n ) : String(n);
		}
	}
}